﻿using Microsoft.Xna.Framework;
using RpgXna.Model.Entities;
using RpgXna.Model.Entities.MainCharacter;
using RpgXna.Model.GameManager;
using RpgXna.Model.Interfaces.ContextMenu;
using RpgXna.Model.Interfaces.Inventory;
using RpgXna.Model.Menu;
using RpgXna.Model.Menu.MenuBehaviors;

namespace RpgXna.Model
{

    public delegate void ItemInteractionEventHandler(Vector2 pos, PickableObject obj);

    public class InteractionsManager
    {
        private readonly Character _c;
        private readonly ContextMenu _cm;
        private readonly CmiFactory _cmi;
        private readonly EnvironmentRepository _env;
        private readonly GameStateManager _gsm;
        private readonly Inventory _inv;

        public InteractionsManager(GameStateManager gsm, ContextMenu cm, EnvironmentRepository env, Character c,
                                   Inventory inv, CmiFactory cmi)
        {
            _gsm = gsm;
            _cm = cm;
            _env = env;
            _c = c;
            _inv = inv;
            _cmi = cmi;

            SubscribeToInteractionRequests();
        }

        private void SubscribeToInteractionRequests()
        {
            _inv.Interactions.Interact += InventoryInteract;
            _env.Interactions.Interact += EnvironmentInteract;
        }

        private void EnvironmentInteract(Vector2 pos, PickableObject obj)
        {
            var cis = new[]
                       {
                           _cmi.Pickup(obj, _inv, _env),
                           _cmi.Leave(),

                       };
            _cm.ConfigureOptions(pos, cis);
        }

        void InventoryInteract(Vector2 pos, PickableObject obj)
        {
            var cis = new[]
                       {
                           _cmi.Leave(),
                           _cmi.Drop(obj, _inv, _env, _c)
                       };
            _cm.ConfigureOptions(pos, cis);
        }


    }
}